package com.chunfeng.service.impl;

import com.chunfeng.entity.Address;
import com.chunfeng.mapper.AddressMapper;
import com.chunfeng.result.JsonRequest;
import com.chunfeng.result.ServiceEnum;
import com.chunfeng.service.IAddressService;
import com.chunfeng.utils.SqlDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 地址业务层实现类
 *
 * @author by 春风能解释
 * <p>
 * 2022/12/20
 */
@Service
@Transactional
@Slf4j
public class AddressServiceImpl implements IAddressService {
    /**
     * 获取当前时间
     */
    private final String date = SqlDateUtils.date;
    /**
     * 地址mapper注入
     */
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查看所有地址信息
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = "address")
    public JsonRequest<List<Address>> lookAllAddresses() {
        List<Address> addresses = addressMapper.selectAllAddress();
        if (addresses.isEmpty()) {
            log.warn("未查询到任何地址信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条地址信息!", addresses.size());
        return JsonRequest.success(addresses);
    }

    /**
     * 按条件获取地址信息
     *
     * @param address 条件
     * @return JSON
     */
    @Override
    @Cacheable(value = "address", key = "#address")
    public JsonRequest<List<Address>> lookAddressesByCondition(Address address) {
        List<Address> addresses = addressMapper.conditionSelectAllAddress(address);
        if (addresses.isEmpty()) {
            log.warn("未查询到任何地址信息!");
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        log.info("已查询到{}条地址信息!", addresses.size());
        return JsonRequest.success(addresses);
    }


    /**
     * 新增一条地址信息
     *
     * @param address 地址信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "address", allEntries = true)
    public JsonRequest<Integer> insertOneAddress(Address address) {
        address.setCreateUser(SqlDateUtils.currentUserId);
        address.setCreateTime(date);
        Integer column = addressMapper.insertAddress(address);
        if (column < 1) {
            log.error("地址信息添加失败!返回值:{}", column);
            return JsonRequest.error(ServiceEnum.INSERT_ERROR);
        }
        log.info("地址信息添加成功!返回值:{}", column);
        return JsonRequest.success(column);
    }

    /**
     * 修改一条地址信息
     *
     * @param address 地址信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = "address", allEntries = true)
    public JsonRequest<Integer> updateOneAddress(Address address) {
        Address address1 = addressMapper.selectAllAddressById(address.getId());
        if (Objects.isNull(address1)) {
            log.warn("未查询到ID为{}的地址信息!", address.getId());
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //注入
        address.setUpdateUser(SqlDateUtils.currentUserId);
        address.setUpdateTime(date);
        Integer column = addressMapper.updateAddressById(address);
        if (column < 1) {
            log.error("ID为{}的地址信息修改失败!返回值:{}", address.getId(), column);
            return JsonRequest.error(ServiceEnum.UPDATE_ERROR);
        }
        log.info("ID为{}的地址信息修改成功!返回值:{}", address.getId(), column);
        return JsonRequest.success(column);
    }

    /**
     * 删除地址信息
     *
     * @param ids ID值
     * @return JSON
     */
    @Override
    @CacheEvict(value = "address", allEntries = true)
    public JsonRequest<Integer> deleteAddress(Integer[] ids) {
        //判断地址信息是否完整
        Integer count = addressMapper.countAddress(ids);
        if (count < ids.length) {
            log.warn("待删除的地址个数与数据库已存在的地址个数不符!数据库实际存在{}个,需要{}个", count, ids.length);
            return JsonRequest.error(ServiceEnum.NOT_FOUND);
        }
        //删除逻辑
        Integer column = addressMapper.deleteAddressById(ids);
        if (column < 1) {
            log.error("删除地址遇到错误!返回值{}", column);
            return JsonRequest.error(ServiceEnum.DELETE_ERROR);
        }
        log.info("已删除{}条地址信息!返回值{}", count, column);
        return JsonRequest.success(column);
    }
}
